package com.lottery.im.module.tim.ui;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.lottery.im.R;

/**
 * 发送语音提示控件
 */
public class VoiceSendingView extends RelativeLayout {

  private AnimationDrawable frameAnimation;
  private final ImageView mImg;
  private final TextView mTvTip;

  public VoiceSendingView(Context context, AttributeSet attrs) {
    super(context, attrs);
    LayoutInflater.from(context).inflate(R.layout.voice_sending, this);
    mImg = (ImageView) findViewById(R.id.microphone);
    mImg.setBackgroundResource(R.drawable.animation_voice);
    mTvTip = (TextView) findViewById(R.id.tv_tip);
    frameAnimation = (AnimationDrawable) mImg.getBackground();
  }

  public void showRecording() {
    mImg.setBackgroundResource(R.drawable.animation_voice);
    frameAnimation.start();
    mTvTip.setText("手指手滑，取消发送");
    mTvTip.setBackgroundColor(ContextCompat.getColor(getContext(),android.R.color.transparent));
  }

  public void wantToCancel(boolean isWant) {
    if (isWant) {
      frameAnimation.stop();
      mImg.setBackgroundResource(R.drawable.ic_cancel_voice);
      mTvTip.setText("松开手指，取消发送");
      mTvTip.setBackgroundColor(ContextCompat.getColor(getContext(),android.R.color.holo_red_dark));
    } else {
      mImg.setBackgroundResource(R.drawable.animation_voice);
      frameAnimation = (AnimationDrawable) mImg.getBackground();
      frameAnimation.start();
      mTvTip.setText("手指上滑，取消发送");
      mTvTip.setBackgroundColor(ContextCompat.getColor(getContext(),android.R.color.transparent));
    }
  }

  public void showCancel() {
    frameAnimation.stop();
  }

  public void release() {
    frameAnimation.stop();
  }
}
